package com.itheima.sh.filterlogin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//1. 登录之后：能够访问hack.html，然后下载黑客视频资源，成为NB黑客；
//2. 未登录：跳转到登录页面，并提示"请先登录"；
//3. 我们对hack.html页面进行过滤，
@WebFilter("/hack.html")
public class loginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
         //从session中获取user对象来判断，如果登录了则放行，没登陆就返回login.html
        User u =(User)request.getSession().getAttribute("user");
          if(u==null){
              response.sendRedirect("/login.html");
          }else{
              chain.doFilter(request,response);
          }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
